home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / New System Software Extensions / QuickDraw™ 3D 1.0 / Samples / SampleCode / Unsupported Libraries / PictRead.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-24  |  4.9 KB  |  222 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2.  **                                                                             **
  3.  **     Module:        PictRead.c                                                 **
  4.  **                                                                          **
  5.  **                                                                          **
  6.  **     Purpose:                                                               **
  7.  **                                                                          **
  8.  **                                                                          **
  9.  **                                                                          **
  10.  **     Copyright (C) 1992-1995 Apple Computer, Inc.  All rights reserved.     **
  11.  **                                                                          **
  12.  **                                                                          **
  13.  *****************************************************************************/
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. #include <StandardFile.h>
  19. #include <Memory.h>
  20.  
  21. #include <QDOffscreen.h>
  22. #include "QD3D.h"
  23. #include "QD3DStorage.h"
  24. #include "PictRead.h"
  25.  
  26.  
  27. /*===========================================================================*\
  28.  *
  29.  *    Routine:    OpenPICTFile()
  30.  *
  31.  *    Comments:    open a PICT file and read it into a PICT handle
  32.  *
  33. \*===========================================================================*/
  34.  
  35. PicHandle OpenPICTFile(
  36.     short     vRefNum, 
  37.     Str255     fName)
  38. {
  39.     short        fRefNum;
  40.     OSErr        err;
  41.     long        curEOF;
  42.     PicHandle    myPic;
  43.     long         count;
  44.     Ptr         buffer;
  45.     
  46.     /* open PICT file */
  47.     err = FSOpen(fName, vRefNum, &fRefNum);
  48.     if (err != 0) {
  49.         /*printf("Error - cannot open file\n"); */
  50.         exit(-1);
  51.     }
  52.     
  53.     /* get size of file */
  54.     err = GetEOF(fRefNum, &curEOF);
  55.     if (err != 0) {
  56.         /*printf("Error - cannot get EOF\n"); */
  57.         exit(-2);
  58.     }
  59.     
  60.     /* move the file mark to 512 */
  61.     err = SetFPos(fRefNum, fsFromStart, 512L);
  62.     if (err != 0) {
  63.         /*printf("Error - cannot seek 512\n"); */
  64.         exit(-3);
  65.     }
  66.  
  67.     /* size of data to read */
  68.     count = curEOF - 512;
  69.     
  70.     /* create the PicHandle */
  71.     myPic = (PicHandle)NewHandle(count);
  72.     HLock((Handle)myPic);
  73.     
  74.     /* read the PICT info */
  75.     buffer = (Ptr)(*myPic);
  76.     err = FSRead(fRefNum, &count, buffer);
  77.     if (err != 0) {
  78.         /*printf("Error - cannot read\n");*/
  79.         exit(-4);
  80.     }
  81.     HUnlock((Handle)myPic);
  82.     
  83.     /* release the file */
  84.     err = FSClose(fRefNum);
  85.     if (err != 0) {
  86.         /*printf("Error - cannot close file \n"); */
  87.         exit(-5);
  88.     }
  89.     
  90.     return (myPic);
  91. }
  92.  
  93.  
  94. /*===========================================================================*\
  95.  *
  96.  *    Routine:    GetPICTFile()
  97.  *
  98.  *    Comments:    Query user for PICT File
  99.  *
  100. \*===========================================================================*/
  101.  
  102. PicHandle GetPICTFile (
  103.     void)
  104. {
  105.     SFReply            reply;
  106.     static    Point    where = { 80, 80 };
  107.     PicHandle         picHandle;
  108.     
  109.     SFGetFile(where, NULL, NULL, -1, NULL, NULL, &reply);
  110.     
  111.     if (reply.good) {
  112.         picHandle = OpenPICTFile(reply.vRefNum, reply.fName);
  113.         return (picHandle);
  114.     }
  115.     
  116.     return (0);
  117. }
  118.  
  119.  
  120. /*===========================================================================*\
  121.  *
  122.  *    Routine:    LoadMapPICT()
  123.  *
  124.  *    Comments:    take a PICT handle and loads it into a bitmap structure
  125.  *
  126. \*===========================================================================*/
  127.  
  128. short LoadMapPICT(
  129.     PicHandle             pict,
  130.     unsigned long         mapID,
  131.     unsigned long         mapSizeX,
  132.     unsigned long         mapSizeY,
  133.     TQ3StoragePixmap     *bMap)
  134. {
  135.     unsigned long             *textureMap;
  136.     unsigned long            *textureMapAddr;
  137.     unsigned long             *pictMap;
  138.     unsigned long            pictMapAddr;
  139.     register unsigned long     row;
  140.     register unsigned long     col;
  141.     Rect                     rectGW;
  142.     GWorldPtr                 pGWorld;
  143.     PixMapHandle             hPixMap;
  144.     unsigned long             pictRowBytes;
  145.     QDErr                    err;
  146.     GDHandle                oldGD;
  147.     GWorldPtr                oldGW;
  148.     short                    success;
  149.     
  150.     mapID;        /* unused argument */
  151.     
  152.     /* save current port */
  153.     GetGWorld(&oldGW, &oldGD);
  154.  
  155.     /* create the GWorld */
  156.     SetRect(&rectGW, 0, 0, (unsigned short)mapSizeX, (unsigned short)mapSizeY);
  157.  
  158.     err = NewGWorld(&pGWorld, 32, &rectGW, 0, 0, useTempMem);
  159.     if (err != noErr)
  160.         return 0;
  161.  
  162.     success = 1;
  163.     
  164.     hPixMap = GetGWorldPixMap(pGWorld);
  165.     pictMapAddr = (unsigned long)GetPixBaseAddr (hPixMap);
  166.     pictRowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;
  167.     
  168.     /* put the PICT into the window */
  169.     SetGWorld(pGWorld, nil);
  170.     
  171.     LockPixels(hPixMap);
  172.     EraseRect(&rectGW);
  173.     DrawPicture(pict, &rectGW);
  174.         
  175.     /* allocate an area of memory for the texture */
  176.     textureMap = (unsigned long *)malloc(mapSizeX * mapSizeY * sizeof(unsigned long));
  177.     if (textureMap == NULL) {
  178.         success = 0;
  179.         goto bail;
  180.     }
  181.     /* bMap->image = (char *)textureMap; */
  182.  
  183.     /* copy the PICT into the texture */
  184.     textureMapAddr = textureMap;
  185.     for (row = 0L; row < mapSizeY; row++) {
  186.         pictMap = (unsigned long *)(pictMapAddr + (pictRowBytes * row));
  187.         for (col = 0L; col < mapSizeX; col++) {
  188.             *textureMap++ = (*pictMap++ | 0xff000000L);
  189.         }
  190.     }
  191.         
  192.     bMap->image = Q3MemoryStorage_New((const unsigned char *)textureMapAddr, 
  193.                                   mapSizeX * mapSizeY * sizeof(unsigned long));
  194.                                   
  195.     if (bMap->image == NULL) {
  196.         /* error */
  197.         success = 0;
  198.         goto bail;
  199.     }
  200.  
  201.     UnlockPixels(hPixMap);
  202.     
  203.     bMap->width     = mapSizeX;
  204.     bMap->height    = mapSizeY;
  205.     bMap->rowBytes     = bMap->width * 4;
  206.     bMap->pixelSize = 32;
  207.     bMap->pixelType    = kQ3PixelTypeRGB32;
  208.     bMap->bitOrder    = kQ3EndianBig;
  209.     bMap->byteOrder    = kQ3EndianBig;
  210.     
  211.     /* Free junk */
  212. bail:
  213.  
  214.     SetGWorld(oldGW, oldGD);
  215.     
  216.     DisposeGWorld(pGWorld);
  217.     if (textureMapAddr != NULL)
  218.         free(textureMapAddr);
  219.     
  220.     return success;
  221. }
  222.